<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="userForm" :rules="rules" label-width="220px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userForm.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <!-- 工号是系统生成的 禁用这个组件 -->
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="userForm.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input v-model="userForm.mobile" size="mini" :disabled="!!this.$route.params.id" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <selectTree v-model="userForm.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userForm.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>

              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker v-model="userForm.timeOfEntry" size="mini" type="date" value-format="yyyy-MM-dd" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker v-model="userForm.correctionTime" size="mini" type="date" value-format="yyyy-MM-dd" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像" prop="staffPhoto">
                <!-- 放置上传图片 -->
                <imageUpload v-model="userForm.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="saveData">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import selectTree from './components/select-tree.vue'
import imageUpload from './components/image-upload.vue'
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'
export default {
  components: {
    selectTree,
    imageUpload
  },
  data() {
    return {
      userForm: {
        username: '', // 员工名字
        workNumber: '', // 工号
        mobile: '', // 手机号
        departmentId: null, // 部门id
        formOfEmployment: null, // 聘用形式
        timeOfEntry: '', // 入职时间
        correctionTime: '', // 转正时间
        staffPhoto: '' // 员工头像
      },
      rules: {
        username: [
          { required: true, message: '员工姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        departmentId: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '转正时间不能为空', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              console.log(new Date(value) >= new Date(this.userForm.timeOfEntry))
              if (new Date(value) >= new Date(this.userForm.timeOfEntry)) {
                callback()
              } else {
                callback(new Error('转正时间不能早于入职时间'))
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    if (this.$route.params.id) {
      this.getEmployeeDetail()
    }
    // this.$route.params.id && this.getEmployeeDetail()
  },
  methods: {
    // 保存更新
    async saveData() {
      // 1. 校验整个表单
      await this.$refs.userForm.validate()
      // console.log(this.userForm)
      if (this.$route.params.id) {
        // 编辑员工
        await updateEmployee(this.userForm)
        this.$message.success('编辑员工信息成功')
      } else {
        // 2. 发送请求
        await addEmployee(this.userForm)
        // 3. 提示用户添加成功
        this.$message.success('添加员工信息成功')
      }

      // 4. 重置表单
      this.$refs.userForm.resetFields()
      // 5. 跳转回员工列表页面
      this.$router.push('/employee')
    },
    // 获取员工详情
    async getEmployeeDetail() {
      this.userForm = await getEmployeeDetail(this.$route.params.id)
    }
  }

}
</script>

  <style scoped lang="scss">
      .edit-form {
        background: #fff;
        padding: 20px;
        .inputW {
          width: 380px
        }
      }

  </style>
